package com.truyen.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.truyen.persistence.dao.LogJsoupDao;
import com.truyen.persistence.entity.LoaiLog;
import com.truyen.persistence.entity.LogJsoup;
import com.truyen.service.LogJsoupService;
import com.truyen.utils.PagingInfo;


@Service
public class LogJsoupServiceImpl implements LogJsoupService {
	
	@Autowired
	private LogJsoupDao logJsoupDao;

	@Override
	public void save(LogJsoup entity) {
		logJsoupDao.save(entity);
	}

	@Override
	public LogJsoup findById(Long id) {
		return logJsoupDao.findById(id);
	}

	@Override
	public List<LogJsoup> getAll() {
		return logJsoupDao.findAll();
	}

	@Override
	public void writeLog(String name, String content, LoaiLog loaiLog,
			boolean isError) {
		Date date = new Date();
		LogJsoup logJsoup = new LogJsoup(name,content,loaiLog,isError,date);
		save(logJsoup);
	}

	@Override
	public List<LogJsoup> getLstLog(String sort, String sortBy,
			LoaiLog loaiLog, PagingInfo pInfo) {
		return logJsoupDao.getLstLog(sort,sortBy,loaiLog,pInfo);
	}

	@Override
	@Transactional
	public void delLst(boolean isError) {
		List<LogJsoup> lst = logJsoupDao.findByProperty("isError", isError);
		if(lst.size() > 0){
			int sizeLst = lst.size();
			for(int i=0;i<sizeLst;i++){
				LogJsoup logJsoup = lst.get(i);
				logJsoupDao.delete(logJsoup);
			}
		}
	}
}
